package com.example.controller;

import com.example.entity.RestBean;
import com.example.entity.vo.request.AiDrawVo;
import com.example.entity.vo.request.CreateNftVo;
import com.example.entity.vo.request.MetaDataVo;
import com.example.service.NFTService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import static com.example.utils.Const.USER_ID;

@Slf4j
@RestController
@RequestMapping("/api/nft")
public class NFTController {
    @Resource
    private NFTService nftService;

    /**
     * 上传图片
     * @param multipartFile 文件
     * @return uri
     */
    @PostMapping("/upload")
    public RestBean<String> createNft(@RequestParam("file") MultipartFile multipartFile) throws Exception{
        if(multipartFile.getSize() > 1024 * 1024 * 100)
            return RestBean.failure(400,"图像图片不能大于100Kb");
        log.info("图片正在上传IPFS...");
        String url = nftService.uploadIPFS
                (multipartFile);
        if(url != null){
            return RestBean.success(url);
        }else {
            return RestBean.failure(400,"图片上传失败，请联系管理员");
        }
    }
    /**
     * 上传元数据
     * @param vo 元数据
     * @return uri
     */
    @PostMapping("/metadata")
    public RestBean<String> uploadMetadata(@RequestBody MetaDataVo vo){
        log.info("元数据正在上传IPFS...");
        String cid = nftService.uploadIPFS
                (vo);
        if(cid != null){
            return RestBean.success(cid);
        }else {
            return RestBean.failure(400,"元数据上传失败，请联系管理员");
        }
    }
    @PostMapping("/ai")
    public RestBean<String> aiDraw(@RequestBody AiDrawVo vo){
        log.info("正在调用ai绘画...");
        String hash = nftService.aiDraw(vo);
        if(hash != null){
            return RestBean.success(hash);
        }else {
            return RestBean.failure(400,"ai绘画失败，请联系管理员");
        }
    }
}
